package com.hmkj.admin.controller.investor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.entity.export.ExportUtils;
import com.hmkj.common.utils.MD5Utils;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.common.utils.ValidateUtils;
import com.hmkj.core.constant.Constants;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.investor.InvestorModel;
import com.hmkj.core.model.investor.InvestorSimulateModel;
import com.hmkj.core.po.commo.Commo;
import com.hmkj.core.po.investor.Investor;
import com.hmkj.core.po.investor.InvestorSimulate;
import com.hmkj.core.po.system.Admin;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.investor.InvestorService;
import com.hmkj.core.service.investor.InvestorSimulateService;
import com.hmkj.core.service.system.AdminService;
import com.hmkj.core.service.user.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class InvestorSimulateJsonController extends GenericController {

    @Resource
    private InvestorService investorService;
    @Resource
    private InvestorSimulateService investorSimulateService;
    @Resource
    private UserService userService;
    @Resource
    private AdminService adminService;

    /**
     * 投资人列表json数据
     */
    @ResponseBody
    @RequestMapping(value = "/investorSimulate/json/list", method = RequestMethod.POST)
    @RequiresPermissions("sys:investorSimulate:list")
    public DtGrid listJson(String dtGridPager) throws Exception {
        DtGrid dtgrid = dataAuthority(dtGridPager, Constants.AG);
        return investorSimulateService.getDtGridList(dtgrid, InvestorSimulateModel.class);
    }

    /**
     * 添加
     *
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping("/investorSimulate/json/add")
    @RequiresPermissions("sys:investorSimulate:add")
    public ResultEntity addJson(InvestorSimulate model) throws Exception {
        if(null == model){
            throw new BussinessException("参数有误");
        }
        if(null == model.getUserId()){
            throw new BussinessException("用户参数有误");
        }
        User user = userService.selectByPrimaryKey(model.getUserId());
        if(null == user){
            throw new BussinessException("指定用户不存在");
        }
        InvestorSimulate investorSimulate1 = investorSimulateService.selectOne(new InvestorSimulate(iv->{
            iv.setUserId(model.getUserId());
        }));
        if(null != investorSimulate1){
            throw new BussinessException("该用户已经被指定为模拟投资人，请勿重复指定");
        }
        //保存
        InvestorSimulate iv = new InvestorSimulate();
        iv.setUserId(model.getUserId());
        investorSimulateService.insert(iv);
        return ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping(value = "/investorSimulate/json/edit")
    @RequiresPermissions("sys:investorSimulate:edit")
    public ResultEntity edit(InvestorSimulate investorSimulate) throws Exception {
        //参数校验
        return ok();
    }

    /**
     * 修改状态
     */
    @ResponseBody
    @RequestMapping(value = "/investorSimulate/json/state")
//    @RequiresPermissions("sys:commo:state")
    public ResultEntity state(Integer id, Integer status) throws Exception {
        //参数校验
        if(null == id || null == status){
            throw new BussinessException("参数错误");
        }
        if(status != InvestorSimulate.STATUS.T1.code && status != InvestorSimulate.STATUS.T2.code){
            throw new BussinessException("状态参数错误");
        }
        InvestorSimulate investorSimulate = investorSimulateService.selectOne(new InvestorSimulate(c -> c.setId(id)));
        if(null ==investorSimulate){
            throw new BussinessException("该模拟投资人不存在");
        }
        investorSimulate.setStatus(status);
        investorSimulateService.updateByIdAndVersionSelective(investorSimulate);
        return ok();
    }

    /**
     * 列表导出
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "investorSimulate/json/export", method = RequestMethod.POST)
    public ResultEntity export(String dtGridPager, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 执行导出
        DtGrid dtgrid = dataAuthority(dtGridPager, Constants.AG);
        dtgrid  =  investorSimulateService.getDtGridListExport(dtgrid, InvestorSimulateModel.class);
        ExportUtils.export(request, response,dtgrid);
        return ok();
    }

}